package com.md24housecall.activity;

import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.md24housecall.manager.SkypeManager;
import com.md24housecall.net.UrlBuilder;
import com.md24housecall.util.GlobalConstant;
import com.md24housecall.util.MyLog;
import com.md24housecall.util.Utils;

public class AddPatientActivity extends FragmentActivity {
	private Button btnBack;
	private EditText edtName, edtPassword, edtIHealth, edtEmail;
	private AQuery mAQuery;
	private Button btnOk, btnCancel;
	private String url = "";
	private GlobalConstant constant = GlobalConstant.getInstance();
	private Utils util = Utils.getInstance();
	private SkypeManager skype=SkypeManager.getInstance();
	private ProgressBar mProgressBar;
	private String name="",email="",ihealthEmail="",password="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_patient);
		mProgressBar=(ProgressBar)findViewById(R.id.progressBar);
		mAQuery = new AQuery(this);
		btnBack = (Button) findViewById(R.id.btnBack);
		edtName = (EditText) findViewById(R.id.edtName);
		edtName.requestFocus();
		showKeyBoard();
		edtEmail = (EditText) findViewById(R.id.edtEmail);
		edtIHealth = (EditText) findViewById(R.id.edtIHealth);
		edtPassword = (EditText) findViewById(R.id.edtPassword);
		
		btnOk = (Button) findViewById(R.id.btnAddPatient);
		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
				name=edtName.getText().toString().trim();
				email=edtEmail.getText().toString().trim();
				ihealthEmail=edtIHealth.getText().toString().trim();
				password=edtPassword.getText().toString().trim();
				
				if(name.length()==0){
					edtName.startAnimation(AnimationUtils.loadAnimation(AddPatientActivity.this, R.anim.shake));
					return;
				}
				if(email.length()==0){
					edtEmail.startAnimation(AnimationUtils.loadAnimation(AddPatientActivity.this, R.anim.shake));
					return;
				}
				
				if(!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()){
					edtEmail.startAnimation(AnimationUtils.loadAnimation(AddPatientActivity.this, R.anim.shake));
					return;
				}
				if(ihealthEmail.length()==0){
					edtIHealth.startAnimation(AnimationUtils.loadAnimation(AddPatientActivity.this, R.anim.shake));
					return;
				}
				if(!android.util.Patterns.EMAIL_ADDRESS.matcher(ihealthEmail).matches()){
					edtIHealth.startAnimation(AnimationUtils.loadAnimation(AddPatientActivity.this, R.anim.shake));
					return;
				}
				if(password.length()==0){
					edtPassword.startAnimation(AnimationUtils.loadAnimation(AddPatientActivity.this, R.anim.shake));
					return;
				}
				
//				url = String.format(UrlBuilder.URL_ADD_PATIENT, 
//						URLEncoder.encode(name,"UTF-8"), URLEncoder.encode(email,"UTF-8"),
//						util.getPreferenceString(constant.KEYUSERID, AddPatientActivity.this, ""),skype.getSkypename(AddPatientActivity.this),URLEncoder.encode(ihealthEmail,"UTF-8"),URLEncoder.encode(password,"UTF-8"));
				url = String.format(UrlBuilder.URL_ADD_PATIENT, 
						name, email,
						util.getPreferenceString(constant.KEYUSERID, AddPatientActivity.this, ""),skype.getSkypename(AddPatientActivity.this),ihealthEmail,password,"UTF-8");
				}catch(Exception e){
					
				}
				
				MyLog.d(GlobalConstant.TAG, "url add patient: "+url);
				hideKeyBoard();
				if(!url.equals("")){
					 mAQuery.progress(mProgressBar).ajax(url, String.class, new AjaxCallback<String>(){
						 @Override
						public void callback(String url, String object,
								AjaxStatus status) {
							// TODO Auto-generated method stub
							if(status.getCode()==200){
								if(object.contains("error")){
									try{
										JSONObject json=new JSONObject(object);
										Toast.makeText(AddPatientActivity.this,json.getString("message") , Toast.LENGTH_LONG).show();
									}catch(Exception e){
										e.printStackTrace();
									}
								}else{
									Toast.makeText(AddPatientActivity.this,getString(R.string.title_add_patient_successful) , Toast.LENGTH_LONG).show();
									constant.isUpdatePatient=true;
									onBackPressed();
								}
							}else{
								Toast.makeText(AddPatientActivity.this, status.getMessage(), Toast.LENGTH_LONG).show();
							}
						}
					 });
				}
			}
		});
		btnCancel = (Button) findViewById(R.id.btnAddIhealth);
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
		btnBack.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});

	}
	
	
	private void hideKeyBoard(){
		InputMethodManager imm = (InputMethodManager)getSystemService(
			      Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(edtEmail.getWindowToken(), 0);
	}
	
	private void showKeyBoard(){
		InputMethodManager imm = (InputMethodManager)getSystemService(
			      Context.INPUT_METHOD_SERVICE);
			imm.showSoftInput(edtName, 0);
	}
}
